package com.tledu.production.controller.crafts;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tledu.production.constant.CommonConstant;
import com.tledu.production.core.result.AjaxResult;
import com.tledu.production.model.crafts.TechnologyModel;
import com.tledu.production.model.crafts.TechnologyPlanModel;
import com.tledu.production.model.crafts.TechnologyRequirementModel;
import com.tledu.production.service.crafts.ITechnologyPlanService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@AllArgsConstructor
@RequestMapping("/technologyPlan")
public class TechnologyPlanController {
    final private ITechnologyPlanService technologyPlanService;
    private  HttpServletRequest request;
    @RequestMapping("/pagePlan")
    @ResponseBody
    //查询
    public AjaxResult<PageInfo<TechnologyRequirementModel>>page(HttpServletRequest req, Integer pageNum, Integer pageSize, String condition, String conditionName){
        if (pageNum == null)pageNum = CommonConstant.PAGENUM;
        if (pageSize == null)pageSize = CommonConstant.PAGESIZE;
        PageHelper.startPage(pageNum,pageSize);
        List<TechnologyRequirementModel> list = null;
        PageInfo<TechnologyRequirementModel> page = null;
        //分页
        if (conditionName==null||"".equals(conditionName)){
            list= technologyPlanService.list();
            page=PageInfo.of(list);
        }else{
            //搜索
            list= technologyPlanService.selectByName(condition,conditionName);
            page=PageInfo.of(list);
        }
        return new AjaxResult<>(page);
    }
    //富文本默认值
    @RequestMapping("/editText/{technologyRequirementId}")
    public String edit(@PathVariable String technologyRequirementId, Model model) {
        request.getSession().setAttribute("technologyRequirementId",technologyRequirementId);
        String text = technologyPlanService.finfEdit(technologyRequirementId);
        model.addAttribute("editText",text);
        return "crafts/edit";
    }
    //富文本保存
    @RequestMapping("/edsave")
    @ResponseBody
    public AjaxResult<TechnologyModel> edsave(String technologyRequirementId,String requirement){

           int res= technologyPlanService.save(technologyRequirementId,requirement);

        if (res == 1) {
            return new AjaxResult("编辑成功");
        } else {
            return new AjaxResult("编辑失败", 500);
        }
    }
    //删除
    @RequestMapping("/delplan")
    @ResponseBody
    public AjaxResult<String> delplan(String technologyRequirementId){
        technologyPlanService.delete1(technologyRequirementId);
        return new AjaxResult<>("删除成功");
    }
    //批量删除
    @GetMapping("/delplanAll")
    @ResponseBody
    public AjaxResult<String> batchDel(String ids){
        String[] idsarr = ids.split(",");
        int i =0;
        for(String s : idsarr){
            technologyPlanService.delete1(s);
            i++;
        }
        if (i >= 1) {
            return new AjaxResult("删除成功");
        } else {
            return new AjaxResult("删除失败", 500);
        }
    }
    //增加
    @RequestMapping("/insert")
    @ResponseBody
    public AjaxResult<Void>add(TechnologyRequirementModel technologyRequirementModel,String requirement){
        technologyRequirementModel.setRequirement(requirement);
        technologyPlanService.insert(technologyRequirementModel);
        return new AjaxResult<>();
    }
    //编辑

    @RequestMapping("/update")
    @ResponseBody
    public AjaxResult<Void>update(TechnologyRequirementModel technologyRequirementModel,String requirement){
        technologyRequirementModel.setRequirement(requirement);
        technologyPlanService.updatereq(technologyRequirementModel);
        return new AjaxResult<>();
    }


}


